package com.atnjupt.java;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

import static com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY;

/**
 * @author Wizarder
 * @create 2021-06-09 22:47
 */
public class RegistServlet extends BaseServlet{

    protected void regist(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取session域中保存的验证码，验证码自动保存到session域中,验证码的Key=com\google\code\kaptcha\Constants.class里面的KAPTCHA_SESSION_KEY
        String imgCode = (String) req.getSession().getAttribute(KAPTCHA_SESSION_KEY);
        //清空session域中的数据
        req.getSession().removeAttribute(KAPTCHA_SESSION_KEY);
        //获取验证码输入框的验证码信息
        String inputCode = req.getParameter("code");
        //把输入的验证码信息和图片验证码信息进行比较
        if(imgCode != null && imgCode.equalsIgnoreCase(inputCode)){

            //获取注册信息
            String username = req.getParameter("username");
            //保存到数据库
            System.out.println("保存到数据库：" + username);
            //请求重定向
            resp.sendRedirect(req.getContextPath()+"/ok.jsp");
        }else{
            System.out.println("请不要重复提交表单");
        }
    }
}
